﻿#include "GroupBoxPropertyEditEntry.h"

#include <QGroupBox>
#include "ObjectPropertyEditEntryFactory.h"
REGISTER_PROPERTY_EDIT_ENTRY(GroupBox)

#include "UiTextEditEntry.h"
#include "FlagsEditEntry.h"
#include "BooleanEditEntry.h"

struct GroupBoxPropertyEditEntry::GroupBoxPropertyEditEntryPrivate {
    QPointer<QGroupBox> object;

    QPointer<UiTextEditEntry> entryTitle;
    QPointer<FlagsEditEntry> entryAlignment;
    QPointer<BooleanEditEntry> entryFlat;
    QPointer<BooleanEditEntry> entryCheckable;
    QPointer<BooleanEditEntry> entryChecked;
};

GroupBoxPropertyEditEntry::~GroupBoxPropertyEditEntry() { }

GroupBoxPropertyEditEntry::GroupBoxPropertyEditEntry(QWidget *parent) : PropertyEditEntry{parent} {
    p.reset(new GroupBoxPropertyEditEntryPrivate);
    initUi();
    retranslateUi();
}

void GroupBoxPropertyEditEntry::setObject(void *object) {
    p->object = static_cast<QGroupBox*>(object);
}

void GroupBoxPropertyEditEntry::onTick() {
    if(!p->object || !isVisible()) {
        return ;
    }

    p->entryTitle->setValue(p->object->title());
    p->entryAlignment->setValue(p->object->alignment());
    p->entryFlat->setValue(p->object->isFlat());
    p->entryCheckable->setValue(p->object->isCheckable());
    p->entryChecked->setValue(p->object->isChecked());
}

void GroupBoxPropertyEditEntry::initUi() {
    p->entryTitle = new UiTextEditEntry;
    appendChildEntry(p->entryTitle);
    connect(p->entryTitle, &UiTextEditEntry::valueChanged, [=](const QString &value) {
        p->object->setTitle(value);
    });

    p->entryAlignment = new FlagsEditEntry;
    appendChildEntry(p->entryAlignment);
    p->entryAlignment->setMetaEnum(QMetaEnum::fromType<Qt::Alignment>());
    connect(p->entryAlignment, &FlagsEditEntry::valueChanged, [=](int value) {
        p->object->setAlignment(value);
    });

    p->entryFlat = new BooleanEditEntry;
    appendChildEntry(p->entryFlat);
    connect(p->entryFlat, &BooleanEditEntry::valueChanged, [=](bool value) {
        p->object->setFlat(value);
    });

    p->entryCheckable = new BooleanEditEntry;
    appendChildEntry(p->entryCheckable);
    connect(p->entryCheckable, &BooleanEditEntry::valueChanged, [=](bool value) {
        p->object->setCheckable(value);
    });

    p->entryChecked = new BooleanEditEntry;
    appendChildEntry(p->entryChecked);
    connect(p->entryCheckable, &BooleanEditEntry::valueChanged, [=](bool value) {
        p->object->setChecked(value);
    });
}

void GroupBoxPropertyEditEntry::retranslateUi() {
    setTitle(tr("QGroupBox"));
    p->entryTitle->setTitle(tr("title"));
    p->entryAlignment->setTitle(tr("alignment"));
    p->entryFlat->setTitle(tr("flat"));
    p->entryCheckable->setTitle(tr("checkable"));
    p->entryChecked->setTitle(tr("checked"));
}
